package com.team.ai.tool;


import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.team.ai.tool.Baidu.Current.Base64Util;
import com.team.ai.tool.Baidu.Current.FileUtil;
import com.team.ai.tool.Baidu.Current.HttpUtil;
import com.team.ai.tool.Baidu.Current.OCRAuthService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

@Component
public class OCRTool {
    @Resource
    OCRAuthService ocrAuthService;//OCR令牌

    /**
     * 百度识图
     * @param Path 图片文件路径
     * @param Type 类型 1.通用型的识别 2.身份证的识别 3.车牌识别
     * @return Map
     */
    public Map<String,String> OCRBase(String Path,int Type){
        Map<String,String>map =new HashMap<>();//返回结果
        try{
            String url = "";//请求地址
            String param = "";//请求参数
            String temp = "";
            byte[] imgData = FileUtil.readFileByBytes(Path);//以比特流的形式读入图片
            String imgStr = Base64Util.encode(imgData);//讲比特流转为Base64编码
            String imgParam = URLEncoder.encode(imgStr, "UTF-8");//设置图片请求参数 字符集为UTF-8
            /*********************根据类型请求接口**********/
            if (Type == 1) { //根据类型 设置请求地址及参数的格式
                url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic";
                param = "image=" + imgParam;
            } else if (Type == 2) {
                url = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard";
                param = "id_card_side=" + "front" + "&image=" + imgParam;
            } else {
                url = "https://aip.baidubce.com/rest/2.0/ocr/v1/license_plate";
                param = "image=" + imgParam;
            }
            /*****************向接口发起请求******************/
            String result= HttpUtil.post(url,ocrAuthService.getAuth(1),param);
            /**************解析接口返回的JSON***************/
            JsonParser jsonParser =new JsonParser();
            JsonObject jsonObject =jsonParser.parse(result).getAsJsonObject();
            if (Type==1){//通用识别
                map.put("result",jsonObject.get("words_result").getAsJsonArray().get(0).toString()
                        .substring(10,jsonObject.get("words_result").getAsJsonArray().get(0).toString().length()-2));
            }else if (Type==2){//身份证识别
                String[] par= new String[]{"姓名","民族","住址","公民身份号码","出生","性别"};
                for (String s:par){
                    temp=jsonObject.getAsJsonObject().getAsJsonObject(s).get("words").toString();
                    map.put(s,temp.substring(1,temp.length()-1));//去掉双引号
                }
            }else if (Type==3){//车牌识别
                    temp=jsonObject.get("words_result").getAsJsonObject().get("number").toString();
                    map.put("车牌",temp.substring(1,temp.length()-1));
                    temp=jsonObject.get("words_result").getAsJsonObject().get("color").toString();
                    map.put("车牌颜色",temp.substring(1,temp.length()-1));
            }
            System.out.println(result);
        }catch (Exception e){
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 动物识别
     * @param filePath 文件路径
     * @return String ：动物名称
     */
    public String Animal(String filePath){
        String url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/animal";
        try{
            byte[] imgData = FileUtil.readFileByBytes(filePath);
            String imgStr = Base64Util.encode(imgData);
            String imgParam = URLEncoder.encode(imgStr, "UTF-8");
            String param = "image=" + imgParam;
            String accessToken = ocrAuthService.getAuth(2);
            String result =HttpUtil.post(url,accessToken,param);//发起请求，获取返回结果
            System.out.println(result);
            /**************解析接口返回的JSON***************/
//            JsonParser jsonParser =new JsonParser();
//            JsonObject jsonObject =jsonParser.parse(result).getAsJsonObject();
//            String temp = jsonObject.get("result").getAsJsonArray().get(0).getAsJsonObject().get("name").toString();
//            temp=temp.substring(1,temp.length()-1);
//            return temp;
            return result;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

}
